//
//  EditProfileHeaderViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/16.
//

import UIKit
import Alamofire
import TZImagePickerController

class EditProfileHeaderViewController: UIViewController,CustomNavgationBar{

    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var headerImageView: UIImageView!
    var userNameText: UITextField!
    
    var headerImage: UIImage?
    var nickName: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createXglView()
        self.creatClearNavigationBar(title: "Edit Profile")
        if let infor = LoginInfor.share.infor {
            self.headerImageView.loadImageWithUrl(infor.header ?? "")
            self.userNameText.text = infor.nickname
        }
        
    }

}

extension EditProfileHeaderViewController{
    @objc private func skipAction(){
        let vc = EditProfileGenderViewController()
        self.navigationController?.pushViewController(vc)
    }
    
    @objc private func editHeaderAction(){
        let imagePick = TZImagePickerController(maxImagesCount: 1, delegate: self)
        imagePick?.allowCrop = false
        imagePick?.maxImagesCount = 1
        imagePick?.modalPresentationStyle = .fullScreen
        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.pingfangSC_Semibold(fontSize: 15),NSAttributedString.Key.foregroundColor: UIColor.hexColor("#80FFE6")], for: UIControl.State.normal)
        self.present(imagePick!, animated: true, completion: nil)
    }
    
    @objc private func nextAction(){
        if let image = self.headerImage {
            self.hudView.hudView.backgroundColor = .clear
            self.hudView.hudView.startAnimation()
            self.view.addSubview(self.hudView)
            self.hudView.snp.makeConstraints { make in
                make.width.centerX.bottom.equalToSuperview()
                make.top.equalTo(PanNavibarH)
            }
            self.uploadHeaderRequest(image: image)
        }else{
            let vc = EditProfileGenderViewController()
            self.navigationController?.pushViewController(vc)
        }
    }
}

extension EditProfileHeaderViewController{
    private func uploadHeaderRequest(image: UIImage){
        PanRequestManager<XGL_RequestBaseResponse<Response>,XGL_CustomRequestError>().request(CustomRequestAPI(requestAPI: .Upload(image: image, file: "file"))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                LoginInfor.share.infor?.header = response.resultJson
                if let name = self.userNameText.text {
                    LoginInfor.share.infor?.nickname = name
                }
                let vc = EditProfileGenderViewController()
                self.navigationController?.pushViewController(vc)
                break
            default:
                break
            }
        }
    }
}

extension EditProfileHeaderViewController:TZImagePickerControllerDelegate{

    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
        if let selectImage = photos.first {
            self.headerImage = selectImage.compressed()
            self.headerImageView.image = selectImage
        }

    }

}
 
extension EditProfileHeaderViewController{
    private func createXglView(){
        self.headerImageView = UIImageView().then({ make in
            make.image = imageWithName("icon_header_default")
            make.layerCornerRadius = 50.0
            make.clipsToBounds = true
        })
        let editIconView = UIImageView().then { make in
            make.image = imageWithName("icon_header_edit")
        }
        let headerButton = UIButton().then { make in
            make.addTarget(self, action: #selector(self.editHeaderAction), for: .touchUpInside)
        }
        self.userNameText = UITextField().then({ make in
            make.textColor = UIColor.white
            make.text = self.nickName
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Ente here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let nameView = LoginUntil.creatLoginInputView(title: "UserName", inputText: self.userNameText)
        let nextButton = UIButton().then { make in
            make.setTitle("Next", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.addTarget(self, action: #selector(self.nextAction), for: .touchUpInside)
        }
        self.view.addSubviews([headerImageView,editIconView,headerButton,nameView,nextButton])
        headerImageView.snp.makeConstraints { make in
            make.width.height.equalTo(100.0)
            make.top.equalTo(conversionWithScrennWidth(value: 147.0))
            make.centerX.equalToSuperview()
        }
        editIconView.snp.makeConstraints { make in
            make.right.bottom.equalTo(headerImageView)
            make.width.height.equalTo(24.0)
        }
        headerButton.snp.makeConstraints { make in
            make.edges.equalTo(headerImageView)
        }
        nameView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.centerX.equalToSuperview()
            make.top.equalTo(headerImageView.snp.bottom).offset(conversionWithScrennWidth(value: 60.0))
        }
        nextButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.bottom.equalTo(-BottomHeight-48.0)
        }
    }
    
    func creatClearNavigationBar(title: String) {
        self.navigationController?.navigationBar.barStyle = .default
        setNeedsStatusBarAppearanceUpdate()
        let backColor = UIColor.clear
        let imageIcon = imageWithName("icon_xgl_back_white")
       
        let naviView = UIView().then { make in
            make.backgroundColor = backColor
        }
        let backItem = MXCustomButton().then { make in
            make.setImage(imageIcon, for: .normal)
            make.buttonSelectAction { [weak self] in
                guard let `self` = self else { return }
                self.backAction()
            }
        }
        let titleView = UILabel().then { make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.text = title
        }
        let skipItem = UIButton().then { make in
            make.addTarget(self, action: #selector(self.skipAction), for: .touchUpInside)
            make.setTitle("Skip", for: .normal)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.backgroundColor = UIColor.hexColor("#001310")
            make.layerCornerRadius = 12.0
        }
        naviView.addSubview(backItem)
        naviView.addSubview(titleView)
        naviView.addSubview(skipItem)
        self.view.addSubview(naviView)
        backItem.snp.makeConstraints { make in
            make.left.equalTo(5.0)
            make.width.height.equalTo(50.0)
            make.bottom.equalTo(-3.0)
        }
        titleView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(backItem)
        }
        skipItem.snp.makeConstraints { make in
            make.centerY.equalTo(titleView)
            make.right.equalTo(-16.0)
            make.width.equalTo(conversionWithScrennWidth(value: 48.0))
            make.height.equalTo(24.0)
        }
        naviView.snp.makeConstraints { make in
            make.centerX.top.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(PanNavibarH)
        }
    }
}
